﻿namespace EFS.RC.ConversationBrowser.Controls.Search
{
	public partial class Criteria : EFS.RC.ConversationBrowser.Controls.Control
	{
		#region Delegates
		/// Search
		public delegate void SearchStartedDelegate();
		public SearchStartedDelegate OnSearchStarted;

		/// SearchCompleted
		public delegate void SearchCompletedDelegate(EFS.RC.ConversationBrowser.Tools.Search.ResultList items);
		public SearchCompletedDelegate OnSearchCompleted;
		#endregion

		#region Constructors
		/// Implicit
		public Criteria()
		{
			InitializeComponent();

			// ignore in design tool
			if (!System.ComponentModel.DesignerProperties.IsInDesignTool)
			{
				UtilitiesService.EventLogs_SearchCompleted += new System.EventHandler<Proxy.EventLogs_SearchCompletedEventArgs>(UtilitiesService_EventLogs_SearchCompleted);
				UtilitiesService.Classifications_GetAllCompleted += new System.EventHandler<Proxy.Classifications_GetAllCompletedEventArgs>(UtilitiesService_Classifications_GetAllCompleted);
			}
		}
		#endregion

		#region Properties
		/// Property HWPartyID: get
		public System.Int64? HWPartyID
		{
			get
			{
				System.Int64 retValue = 0;
				if (System.Int64.TryParse(textBoxHWPartyID.Text, out retValue))
				{
					return retValue;
				}

				// none
				return null;
			}
		}

		/// Property HWApplicantID: get
		public System.Int64? HWApplicantID
		{
			get
			{
				System.Int64 retValue = 0;
				if (System.Int64.TryParse(textBoxHWApplicantID.Text, out retValue))
				{
					return retValue;
				}

				// none
				return null;
			}
		}
		#endregion

		#region LoadData
		/// LoadData
		private void LoadData()
		{
			if (!System.ComponentModel.DesignerProperties.IsInDesignTool)
			{
				UtilitiesService.Classifications_GetAllAsync();
			}
		}
		#endregion

		#region Events
		/// Loaded
		private void criteria_Loaded(object sender, System.Windows.RoutedEventArgs e)
		{
			LoadData();
		}

		/// Search
		private void buttonSearch_Click(object sender, System.Windows.RoutedEventArgs e)
		{
			// show wait
			EFS.Common.UI.Controls.NotificationDialog.ProgressDialog.InProgress(this, "Searching ..");

			// selected classification
			EFS.RC.CC.CH.Entities.Classification selectedClassification = (EFS.RC.CC.CH.Entities.Classification)comboBoxClassification.SelectedItem;

			System.Int64? classificationId = null;
			if (selectedClassification != null)
			{
				classificationId = selectedClassification.ID.Value;
			}

			//time
			System.Int32? minuteFrom = null;
			System.Int32 minute;
			System.Boolean parse =  System.Int32.TryParse(textBoxDurationFromMinutes.Text, out minute);
			if (parse)
			{
				minuteFrom = minute;
			}

			System.Int32? minuteTo = null;
			parse = System.Int32.TryParse(textBoxDurationToMinutes.Text, out minute);
			if (parse)
			{
				minuteTo = minute;
			}
 
			// call
			UtilitiesService.EventLogs_SearchAsync(
				EFS.Common.Functions.String.GetTrimmedString(textBoxAgentFirstname.Text),
				EFS.Common.Functions.String.GetTrimmedString(textBoxAgentLastname.Text),
				EFS.Common.Functions.String.GetTrimmedString(textBoxSupervisorFirstname.Text),
				EFS.Common.Functions.String.GetTrimmedString(textBoxSupervisorLastname.Text),
				datePickerFrom.SelectedDate,
				datePickerTo.SelectedDate,
				EFS.Common.Functions.String.GetTrimmedString(textBoxHWFirstName.Text),
				EFS.Common.Functions.String.GetTrimmedString(textBoxHWLastName.Text),
				EFS.Common.Functions.String.GetTrimmedString(textBoxCustomerUri.Text),
				HWPartyID,
				HWApplicantID,
				classificationId,
				minuteFrom,
				minuteTo);

			// call registered delegates
			if (null != OnSearchStarted)
			{
				this.OnSearchStarted();
			}
		}
		#endregion

		#region WS Callbacks
		/// Conversation_SearchCompleted
		private void UtilitiesService_EventLogs_SearchCompleted(object sender, Proxy.EventLogs_SearchCompletedEventArgs e)
		{
			// call registered delegates
			// close wait
			EFS.Common.UI.Controls.NotificationDialog.ProgressDialog.Completed(this);

			try
			{
				// verify
				if (null != e.Error)
				{
					throw e.Error;
				}

				// deserialize
				EFS.RC.ConversationBrowser.Tools.Search.ResultList resultList =
					EFS.Common.Serialization.Functions.Deserialize<EFS.RC.ConversationBrowser.Tools.Search.ResultList>(e.Result.ToString());

				// call registered delegates
				if (null != this.OnSearchCompleted)
				{
					this.OnSearchCompleted(resultList);
				}
			}
			catch (System.Exception ex)
			{
				ShowErrorMessage("Failed to retrieve events.", ex);
			}
		}

		// UtilitiesService_Classifications_GetAllCompleted
		private void UtilitiesService_Classifications_GetAllCompleted(object sender, Proxy.Classifications_GetAllCompletedEventArgs e)
		{
			// call registered delegates
			// close wait
			EFS.Common.UI.Controls.NotificationDialog.ProgressDialog.Completed(this);

			try
			{
				// verify
				if (null != e.Error)
				{
					throw e.Error;
				}

				// deserialize
				EFS.RC.CC.CH.Entities.Classification[] resultList =
					EFS.Common.Serialization.Functions.Deserialize<EFS.RC.CC.CH.Entities.Classification[]>(e.Result.ToString());

				comboBoxClassification.ItemsSource = resultList;

			}
			catch (System.Exception ex)
			{
				ShowErrorMessage("Failed to retrieve events.", ex);
			}
		}
		#endregion
	}

}